package gui.jfram;

import bll.model.UserModel;
import bll.service.UserService;
import bll.serviceimpl.UserServiceImpl;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class RegFrame extends JFrame {
    private JLabel jLabel1;
    private JTextField jTextField1;
    private JLabel jLabel2;
    private JPasswordField jPasswordField1;
    private JButton jButton1;
    private JButton jButton2;

    public RegFrame()  {
        jLabel1 = new JLabel("用户名：");
        jTextField1 = new JTextField(20);
        jLabel2 = new JLabel("密码");
        jPasswordField1 = new JPasswordField(20);
        jButton1 = new JButton("确定");
        jButton2 = new JButton("取消");
        this.setLayout(new GridLayout(3,2));
        this.add(jLabel1);
        this.add(jTextField1);
        this.add(jLabel2);
        this.add(jPasswordField1);
        this.add(jButton1);
        this.add(jButton2);
        this.setTitle("注册");
        this.setSize(300, 200);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setVisible(true);

        jButton1.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                UserService userService = new UserServiceImpl();
                UserModel userModel = new UserModel(jTextField1.getText(),jPasswordField1.getText());
                if(userService.register(userModel)){
                    RegFrame.this.setVisible(false);
                    MainFrame mainFrame = new MainFrame();
                    JOptionPane.showMessageDialog(RegFrame.this,"注册成功");
                    RegFrame.this.dispose();
                }else{
                    JOptionPane.showMessageDialog(RegFrame.this,"注册失败");
                }
            }
        });
    }
}
